package com.intellagric.service.impl.module.loganalyse;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.LogAnalyzeJobConditionMapper;
import com.intellagric.mapper.LogAnalyzeJobMapper;
import com.intellagric.pojo.LogAnalyzeJob;
import com.intellagric.pojo.LogAnalyzeJobConditionExample;
import com.intellagric.pojo.LogAnalyzeJobExample;
import com.intellagric.pojo.LogAnalyzeJobKey;
import com.intellagric.service.impl.module.loganalyse.utils.MyShardedJedisPool;
import com.intellagric.service.module.loganalyse.LogAnalyseJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;

import java.util.List;

/**
 * @Auther: zhy
 * @Date: 2019/5/11
 * @Description: 日志分析任务服务
 */
@Service
public class LogAnalyseJobServiceImpl implements LogAnalyseJobService {

    @Autowired
    private LogAnalyzeJobMapper logAnalyzeJobMapper;

    @Autowired
    private LogAnalyzeJobConditionMapper logAnalyzeJobConditionMapper;

    /**
    * 添加日志分析任务信息
    * @param logAnalyzeJob
    * @return int
    */
    public ResponseMessage addLogAnalyzeJob(LogAnalyzeJob logAnalyzeJob) {
        if (logAnalyzeJobMapper.insertSelective(logAnalyzeJob) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除日志分析任务信息，并且删除对应的日志规律规则
     * @param id
     * @return int
     */
    public ResponseMessage deleteLogAnalyzeJob(int id,String jobName) {
        LogAnalyzeJobKey logAnalyzeJobKey = new LogAnalyzeJobKey();
        logAnalyzeJobKey.setJobid(id);
        logAnalyzeJobKey.setJobname(jobName);

        if (logAnalyzeJobMapper.deleteByPrimaryKey(logAnalyzeJobKey) == 1) {
            //删除日志分析任务详细
            LogAnalyzeJobConditionExample logAnalyzeJobConditionExample = new LogAnalyzeJobConditionExample();
            logAnalyzeJobConditionExample.createCriteria().andJobidEqualTo(id);
            logAnalyzeJobConditionMapper.deleteByExample(logAnalyzeJobConditionExample);

            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改日志分析任务信息
     * @param logAnalyzeJob
     * @return int
     */
    public ResponseMessage editLogAnalyzeJob(LogAnalyzeJob logAnalyzeJob) {
        LogAnalyzeJobExample logAnalyzeJobExample = new LogAnalyzeJobExample();
        logAnalyzeJobExample.createCriteria().andJobidEqualTo(logAnalyzeJob.getJobid());

        if (logAnalyzeJobMapper.updateByExampleSelective(logAnalyzeJob, logAnalyzeJobExample) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询日志分析任务
     * @param id
     * @return LogAnalyzeJob
     */
    public LogAnalyzeJob queryLogAnalyzeJobById(int id,String jobName) {
        LogAnalyzeJobKey logAnalyzeJobKey = new LogAnalyzeJobKey();
        logAnalyzeJobKey.setJobid(id);
        logAnalyzeJobKey.setJobname(jobName);
        return logAnalyzeJobMapper.selectByPrimaryKey(logAnalyzeJobKey);
    }

    /**
     * 通过业务线编号查询日志分析任务
     * @param businessId
     * @return List<LogAnalyzeJob>
     */
    public LayuiDataGridResult queryLogAnalyzeJobByBusinessId(int businessId, String date) {
        LogAnalyzeJobExample logAnalyzeJobExample = new LogAnalyzeJobExample();
        logAnalyzeJobExample.createCriteria().andBusinessidEqualTo(businessId);
        List<LogAnalyzeJob> logAnalyzeJobList = logAnalyzeJobMapper.selectByExample(logAnalyzeJobExample);

        //查询job下对应的pv数量
        ShardedJedis jedis = MyShardedJedisPool.getShardedJedisPool().getResource();
        String format = date.replaceAll("-", "");
        for (LogAnalyzeJob logAnalyzeJob : logAnalyzeJobList) {
            //查询redis数据库
            String pvKey = "log:" + logAnalyzeJob.getJobname() + ":pv:" + format;
            String pv = jedis.get(pvKey);
            if (pv != null && !"".equals(pv)) {
                logAnalyzeJob.setCount(Integer.parseInt(pv));
            } else {
                logAnalyzeJob.setCount(0);
            }
        }
        return LayuiDataGridResult.success().add(logAnalyzeJobList,logAnalyzeJobList.size());
    }

    /**
     * 查询所有日志分析任务信息
     * @return List<LogAnalyzeJob>
     */
    public LayuiDataGridResult queryLogAnalyzeJobList() {
        LogAnalyzeJobExample LogAnalyzeJobExample = new LogAnalyzeJobExample();
        LogAnalyzeJobExample.createCriteria().andJobidIsNotNull();
        List<LogAnalyzeJob> logAnalyzeJobList = logAnalyzeJobMapper.selectByExample(LogAnalyzeJobExample);
        return LayuiDataGridResult.success().add(logAnalyzeJobList,logAnalyzeJobList.size());
    }

    /**
     * 分页查询所有日志分析任务记录
     * @return List<LogAnalyzeJob>
     */
    public LayuiDataGridResult queryLogAnalyzeJobPage(int page, int rows) {
        PageHelper.startPage(page, rows);
        LogAnalyzeJobExample LogAnalyzeJobExample = new LogAnalyzeJobExample();
        LogAnalyzeJobExample.createCriteria().andJobidIsNotNull();
        List<LogAnalyzeJob> logAnalyzeJobList = logAnalyzeJobMapper.selectByExample(LogAnalyzeJobExample);
        return LayuiDataGridResult.success().add(logAnalyzeJobList,logAnalyzeJobList.size());
    }


}
